package com.zixue.heima.progressNetty;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

import java.util.Random;

/**
 * @ClassName : nianbaoFixClinet
 * @Description : TODO
 * @Author : chunxiong
 * @Date : 2025/5/13 17:37
 * @Version 1.0
 */
public class nianbaoFixClinet {

    //发送len个c ，不足10 用-补齐
    public static String file10Bytes(String s, int len){
        String result ="";
        for (int i = 0; i < len; i++) {
            result += s;
        }
        for (int i = 0; i < 10 - len; i++) {
            result += "-";
        }
        System.out.println("result:"+result);
        return result;
    }

    public static void main(String[] args) throws InterruptedException {

//        System.out.println(file10Bytes("1",2));
//        System.out.println(file10Bytes("2",6));
//        System.out.println(file10Bytes("3",10));
        send();
    }

    public static void send() throws InterruptedException {
        NioEventLoopGroup worker = new NioEventLoopGroup();
        try {
            new Bootstrap()
                    .group(worker)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<NioSocketChannel>() {
                        @Override
                        protected void initChannel(NioSocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
                            ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                            //会在连接channel 建立成功后、触发active事件
                                @Override
                                public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                    ByteBuf byteBuf = ctx.alloc().buffer();
                                    Random random = new Random();
                                    for (int i = 0; i < 10; i++) {
                                        int randomInt = random.nextInt(9)+1;
//                                        System.out.println("i:"+i+"randomInt:"+randomInt);
                                        String s = file10Bytes(String.valueOf(i), randomInt);
                                        byteBuf.writeBytes(s.getBytes());
                                    }
                                    ctx.writeAndFlush(byteBuf);
                                }
                            });
                        }
                    }).connect("localhost",8080)
                    .channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            worker.shutdownGracefully();
        }

    }
}
